#!/usr/bin/env bash
#
# Author: Jasmin Blanchette
#
# DESCRIPTION: Isabelle tactics for TPTP competitive division


PRG="$(basename "$0")"

function usage() {
  echo
  echo "Usage: isabelle $PRG TIMEOUT FILES"
  echo
  echo "  Runs a combination of Isabelle tactics on TPTP problems."
  echo "  Each problem is allocated at most TIMEOUT seconds."
  echo
  exit 1
}

[ "$#" -eq 0 -o "$1" = "-?" ] && usage

SCRATCH="Scratch_${PRG}_$$_${RANDOM}"

TIMEOUT=$1
shift

isabelle build -b HOL-TPTP 2>&1 | grep --line-buffered -v "elapsed time$"

for FILE in "$@"
do
  echo "theory $SCRATCH imports \"$TPTP_HOME/ATP_Problem_Import\" begin \
ML {* ATP_Problem_Import.isabelle_tptp_file @{theory} ($TIMEOUT) \"$FILE\" *} end" \
    > /tmp/$SCRATCH.thy
  isabelle process -e "use_thy \"/tmp/$SCRATCH\"; exit 1;" -l HOL-TPTP | grep --line-buffered -v "^###\|^PROOF FAILED for depth\|^Failure node\|inferences so far.  Searching to depth\|^val \|^Loading theory\|^poly.*warning: The type of\|^   monotype.$"
done
